<#include "../include/header.html">
<script src="${base.contextPath}/lib/bootstrap-3.3.7/js/bootstrap.min.js"></script>
<body>
<script type="text/javascript">
    var viewModel = Hap.createGridViewModel("#jobGrid");
</script>
<div id="dialog" style="display: none"></div>
<div id="page-content">
    <div class="pull-left" id="toolbar-btn" style="padding-bottom:10px;">
        <div class="btn-group">
            <button  id="scheduler" type="button" class="btn btn-default dropdown-toggle fa fa-bolt" data-toggle="dropdown" style="float:left;width:100px;margin-right:5px;" ><@spring.message "job.scheduler"/><span class="caret"></span>
            </button>
            <ul class="dropdown-menu" role="menu">
                <li><a href="javascript:schedulerOperation('start')"  style="color:#00CC00;" class="fa fa-play"><span style="margin-left: 10px;"><@spring.message "hap.resume"/></span></a></li>
                <li><a href="javascript:schedulerOperation('standby')" style="color:#FFCC00;" class="fa fa-pause"><span style="margin-left: 10px;"><@spring.message "hap.pause"/></span></a></li>
            </ul>
        </div>
        <div class="btn-group">
            <button type="button" class="btn btn-primary dropdown-toggle fa fa-plus"
                    data-toggle="dropdown" style="float:left;width:120px;margin-right:5px;" ><@spring.message "job.newjob"/><span class="caret"></span>
            </button>
            <ul class="dropdown-menu" role="menu">
                <li><a style="cursor: pointer" onclick="newsimplejob()"><@spring.message "job.newsimplejob"/></a></li>
                <li><a style="cursor: pointer" onclick="newcronjob()" ><@spring.message "job.newcronjob"/></a></li>
            </ul>
        </div>
        <div class="btn-group">
            <button type="button" class="btn btn-info dropdown-toggle fa fa-hand-o-right"
                    data-toggle="dropdown"  style="float:left;width:100px;margin-right:5px;" >
                <@spring.message "hap.action"/> <span class="caret"></span>
            </button>
            <ul class="dropdown-menu" role="menu">
                <li><a style="color:#FFCC00;cursor: pointer" class="fa fa-pause"onclick="pause()" ><span style="margin-left: 10px;"><@spring.message "hap.pause"/></span></a></li>
                <li><a style="color:#00CC00;cursor: pointer" class="fa fa-play"onclick="resume()" ><span style="margin-left: 10px;"><@spring.message "hap.resume"/></span></a></li>
                <li><a style="color:#FF3333;cursor: pointer" class="fa fa-remove" data-bind="click:remove" ><span style="margin-left: 11px;"><@spring.message "hap.delete"/></span></a></li>
            </ul>
        </div>
    </div>
    <script>kendo.bind($('#toolbar-btn'), viewModel);</script>
    <div class="pull-right" id="query-form" style="padding-bottom:10px;">
        <input type="text" data-role="maskedtextbox" style="float:left;width:150px;margin-right:5px;" placeholder='<@spring.message "jobdetaildto.jobname"/>' data-bind="value:model.jobName"
               class="k-textbox">
        <input type="text" data-role="maskedtextbox" style="float:left;width:150px;margin-right:5px;" placeholder='<@spring.message "jobdetaildto.jobgroup"/>' data-bind="value:model.jobGroup"
               class="k-textbox">
        <span class="btn btn-primary fa fa-search" style="float:left;width:70px;" data-bind="click:query" type="submit"><span style="margin-left: 3px;"><@spring.message "hap.query"/></span></span>
        <div style="clear:both"></div>
    </div>
    <script>kendo.bind($('#query-form'), viewModel);</script>
    <div style="clear:both">
        <div id="jobGrid"></div>
    </div>
</div>


<script type="text/javascript">
    Hap.initEnterQuery("#query-form",viewModel.query);

    function schedulerOperation(action) {
        $.ajax({
            url    : '${base.contextPath}/job/scheduler/' + action,
            success: function (json) {
                var row  = json.rows[0];
                 if(row.inStandbyMode == true){
                     $("#scheduler").css("background-color","#FFCC00");
                 }else if(row.started==true){
                     $("#scheduler").css("background-color","#00CC00");
                 }
                viewModel.query();
            }
        });
    }

    (function(){
        $.ajax({
            url    : '${base.contextPath}/job/scheduler/' + "info",
            success: function (json) {
                var row  = json.rows[0];
                if(row.inStandbyMode==true){
                    $("#scheduler").css("background-color","#FFCC00");
                }else if(row.started==true){
                    $("#scheduler").css("background-color","#00CC00");
                }
            }
        });
    })();

    function joboperation_ajax(obj, url, successCallback) {
        $.ajax({
            url: url,
            type: "POST",
            dataType: "json",
            contentType: "application/json",
            data: kendo.stringify(obj),
            success: function (json) {
                if (successCallback) {
                    successCallback();
                }
            },
            error: function () {
                grid.refresh();
            }
        });
    }

    function openJobDetailPage(jobName, triggerType, jobGroup) {
        var url = '${base.contextPath}/job/job_infodetail.html?jobName=' + jobName + '&triggerType=' + triggerType + '&jobGroup=' + jobGroup;
        var title =  '<@spring.message "job.jobdetail"/>';
        var dialog = Hap.createWindow('#dialog',{
            width: 800,
            height: '80%',
            url: url
        });
        dialog.title(title);
        if(parent.autoResizeIframe){
            parent.autoResizeIframe('${RequestParameters.functionCode!}', 800, function(){
                dialog.center().open();
            })
        } else {
            dialog.center().open();
        }

    }

    window.stateColor = {
        "PAUSED": "color:#FFCC00;",
        "NORMAL": "color:#00CC00;",
        "COMPLETE": "color:grey;",
        "ERROR": "color:#FF3333;",
        "BLOCKED": "color:black;",
        "NONE": "color:black;"
    };
    // $(document).ready(function () {


    var crudServiceBaseUrl = '${base.contextPath}/job',
            dataSource = new kendo.data.DataSource({
                transport: {
                    read: {
                        url: crudServiceBaseUrl + "/query",
                        type: "POST",
                        dataType: "json"
                    },
                    destroy: {
                        url: crudServiceBaseUrl + "/deletejob",
                        type: "POST",
                        dataType: "json",
                        contentType: 'application/json'
                    },
                    parameterMap: function (options, type) {
                        if (type !== "read" && options.models) {
                            return kendo.stringify(Hap.prepareSubmitParameter(options, type));
                        } else if (type === "read") {
                            return Hap.prepareQueryParameter(viewModel.model.toJSON(), options)
                        }
                    }
                },
                batch: true,
                serverPaging: true,
                pageSize:20,
                schema: {
                    data: 'rows',
                    total: 'total',
                    model: {
                        id: "jobName"
                    }
                }
            });


    var grid = $("#jobGrid").kendoGrid({
        dataSource: dataSource,
        dataBound: function () {
            if(parent.autoResizeIframe){
                parent.autoResizeIframe('${RequestParameters.functionCode!}')
            }
        },
        navigatable: false,
        resizable: true,
        scrollable: true,
        editable: false,
        selectable: 'multiple,rowbox',
        pageable: {
            pageSizes: [5, 10, 20, 50],
            refresh: true,
            buttonCount: 5
        },
        columns: [
            {
                field: "jobName", title: '<@spring.message "jobdetaildto.jobname"/>',
                width: 120,
                template: function (item) {
                    var jobName = item.jobName;
                    var triggerType = item.triggerType;
                    var jobGroup = item.jobGroup;
                    return Hap.createAnchor("<span style='text-decoration: none;" + (stateColor[item.runningState]) + "'>" + jobName + "</span>",
                            openJobDetailPage, jobName, triggerType, jobGroup);
                }
            },
            {
                field: "runningState",
                title: '<@spring.message "jobrunninginfodto.jobstatus"/>',
                width: 80
            },
            {
                field: "jobGroup",
                title: '<@spring.message "jobdetaildto.jobgroup"/>',
                width: 80

            }, {
                field: "jobClassName",
                title: '<@spring.message "jobdetaildto.jobclassname"/>',
                width: 200
            }, {
                field: "description",
                title: '<@spring.message "jobdetaildto.description"/>',
                width: 200
            }, {
                field: "previousFireTime",
                title: '<@spring.message "jobrunninginfodto.previousfiretime"/>',
                width: 160,
                format: "{0:yyyy-MM-dd HH:mm:ss}",
                headerAttributes: {
                    "class": "table-header-cell",
                    style  : "text-align: center"
                },
                attributes:{
                    style  : "text-align: center"
                }
            }, {
                field: "nextFireTime",
                title: '<@spring.message "jobrunninginfodto.nextfiretime"/>',
                width: 160,
                headerAttributes: {
                    style  : "text-align: center"
                },
                attributes:{
                    style  : "text-align: center"
                }
            }
        ]
    }).data("kendoGrid");

    function newcronjob() {

        var url = '${base.contextPath}/job/job_cron_add.html';
        var title = '<@spring.message "job.jobdetail"/>';
        var dialog = Hap.createWindow('#dialog',{
            width: 800,
            height: '80%',
            url: url
        });
        dialog.title(title);
        if(parent.autoResizeIframe){
            parent.autoResizeIframe('${RequestParameters.functionCode!}', 800, function(){
                dialog.title('<@spring.message "job.newcronjob"/>').center().open();
            })
        } else {
            dialog.title('<@spring.message "job.newcronjob"/>').center().open();
        }


    }

    function newsimplejob() {
        var url = '${base.contextPath}/job/job_simple_add.html';
        var title = '<@spring.message "job.jobdetail"/>';
        var dialog = Hap.createWindow('#dialog',{
            width: 800,
            height: '80%',
            url: url
        });
        dialog.title(title);

        if(parent.autoResizeIframe){
            parent.autoResizeIframe('${RequestParameters.functionCode!}', 800, function(){
                dialog.title('<@spring.message "job.newsimplejob"/>').center().open();
            })
        } else {
            dialog.title('<@spring.message "job.newsimplejob"/>').center().open();
        }

    }

    function pause() {
        var checked = grid.selectedDataItems();
        if (grid.selectedDataItems().length) {
           joboperation_ajax(checked, '${base.contextPath}/job/pause', function () {
                $.each(checked, function (i, v) {
                    grid.dataSource.get(v.jobName).runningState = "PAUSED";
                })
                $("#jobGrid").data('kendoGrid').dataSource.read(1);
            });
        }

    }

    function resume() {
        var checked = grid.selectedDataItems();
        if (grid.selectedDataItems().length) {
            joboperation_ajax(checked, '${base.contextPath}/job/resume', function () {
                $.each(checked, function (i, v) {
                    grid.dataSource.get(v.jobName).runningState = "NORMAL";
                })
                $("#jobGrid").data('kendoGrid').dataSource.read(1);
            });
        }

    }



</script>
</body>
</html>
